<template>
  <!-- 历史告警记录 -->
  <BaseMainSlot :show-tab="true">
    <!-- <template v-slot:zhactbar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-edit="false"
        @add="add"
        @cancel="cancel"
        @del="del"
        @save="save"
      />
    </template>-->
    <template v-slot:btn-box>
      <ActionBar
        :page-no="297"
        :has-add="false"
        :has-edit="false"
        :has-cancel="false"
        :has-edit-column="true"
        :has-export="true"
        @showColumn="showColumn"
        @download="download"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <Table @sortChange="changeTableSort" :propList="tableColumn" v-loading="loading" :listData="tableData" :showSelectColumn="false"
        @handleSelectionChange="handleSelectionChange">
        <template v-slot:operation="{ row }">
          <template v-if="!row.index">
            <el-button
              v-if="row.alarmMessageId"
              type="success"
              plain
              size="small"
              @click="getalrmmessage(row)"
            >查看详情</el-button>
          </template>
        </template>
        <template v-slot:disposeStatus="{ row }">
          <template v-if="!row.index">
            <div :style="{ color: row.disposeStatus === true ? '#5cb87a' : '#f56c6c' }" >
              {{ filtername(row.disposeStatus) }}
            </div>
          </template>
        </template>
        <template v-slot:lookFacilityList="{ row }">
            <template v-if="!row.index">
              <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="lookFacilityList(row)"
            >
              <i class="el-icon-view"></i> 查看
            </el-button>
            </template>
          </template>
      </Table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        id="tableColumnDialog"
        title="配置列"
        :visible.sync="dialogTableColumnVisible"
      >
        <tableColumn
          v-if="dialogTableColumnVisible"
          ref="tableColumn"
          :tableColumn="allTableColumn"
          @cancelColumn="cancelColumn"
        />
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="dialogTableColumnVisible = false">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="saveColumn"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <SeeEquip
        :is-show-equip="isShowEquip"
        :equip-data="facilityBOList"
        @closeDialog="isShowEquip = false"
      />
    </template>
  </BaseMainSlot>
</template>
<script>
import SeeEquip from "@/components/seeEquip/index.vue";
import SearchForm from '@/components/searchForm';
import Table from '@/components/table/table.vue';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import selectbut from "@/components/selectbutton/index.vue";
import nowpagereqs from "@/api/digital-production/monitoring-alarm/alarm-category-management";
import nowpagereq from "@/api/digital-production/monitoring-alarm/historical-alarm-record/index";
import alarmNowpagereq from "@/api/digital-production/monitoring-alarm/alarm-category-management/index";
import userreq from "@/api/user/index";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import { mymixin } from "@/utils/mixin";
export default {
  name: "HistoricalAlarmRecord",
  components: { SearchForm, Table, ActionBar, Pagination, BaseMainSlot,SeeEquip, selectbut },
  mixins: [mymixin],
  data() {
    return {
      isShowEquip: false,
      facilityBOList: [],//设施列表
      tableColumn: [
        { prop: 'batchName', label: '批次名称', minWidth: '300' },
        {
          prop: "lookFacilityList",
          label: "设施列表",
          slotName: "lookFacilityList",
          minWidth: "180",
        },
        { prop: 'baseAlarmName', label: '告警名称', minWidth: '300' },
        { prop: 'dataItemName', label: '告警数据项', minWidth: '150' },
        { prop: 'alarmPresetValue', label: '预设告警值', minWidth: '130' },
        { prop: 'actualAlarmValue', label: '实际告警值', minWidth: '130' },
        { prop: 'generateTime', label: '发生时间', minWidth: '180' },
        { prop: 'dataTypeName', label: '告警数据类型', minWidth: '150' },
        { prop: 'disposeStatus', label: '告警信息状态', slotName: 'disposeStatus', minWidth: '150' },
        { prop: 'disposeTime', label: '处理时间', minWidth: '180' },
        {prop: 'disposeContent',label: '处理意见',minWidth:'180'},
        { prop: 'operation', label: '操作', slotName: 'operation', minWidth: "120" }
      ],
      formConfig: [
        {
          label: '选择批次',
          prop: 'batchNo',
          type: 'select',
          options: [], // batchList
          optionKey: { label: 'batchName', value: 'batchNo' },
        }, {
          label: '设施名称',
          prop: 'facilityNo',
          type: 'select',
          options: [], // facilityList
          optionKey: { label: 'facilityName', value: 'facilityNo' }
        }, {
          label: '告警数据类型',
          prop: 'alarmTypeId',
          type: 'select',
          options: [], // alarmTypeList
          optionKey: { label: 'alarmTypeName', value: 'alarmTypeId' }
        }, {
          label: '数据类型',
          prop: 'dataTypeId',
          type: 'select',
          options: [], // dataTypeList
          optionKey: { label: 'dataTypeName', value: 'dataTypeId' }
        }, {
          label: '选择数据项',
          prop: 'dataItemId',
          type: 'select',
          options: [], // dataitemlist
          optionKey: { label: 'originalName', value: 'dataItemId' }
        }, 
        {
          label: '选择时间',
          prop: 'date',
          type: 'daterange',
          format: 'yyyy-MM-dd HH:mm:ss'
        },
      ],
      // 可选择数据类型
      dataTypeList: [],
      // 可选择批次
      batchList: [],
      nowselecttype: 0, // 0为物联自动告警 1为手动记录告警 2为数值告警
      dataitemlist: [],
      searchlist: {
        dataItemId: "",
        date: ""
      },
      // 选中的数据项数组 employeeId组成 (此数组最多为一个)
      // 可选择所有地块
      alarmTypeList: [],
      nowselectedList: [],
      // 设施
      facilityList: [],
      facilityNo: "",
    };
  },
  watch: {},
  created() {},
  mounted() {
    this.getselecltlist();
    this.search();
  },
  methods: {
    changeTableSort(column) {
      this.sortList = column.order ? [
        { property: column.prop, direction: column.order == "ascending" ? 'asc' : 'desc' }
      ] : []
      this.getTableDataList()
    },
    // 表格中的标签
    filterTag(value, row) {
      return row.disposeStatus === value;
    },
    // 表格中的标签
    filtername(value) {
      if (value) {
        return "已完成";
      } else {
        return "未完成";
      }
    },
    // 获取不同类型的表格数据
    // gettabletype(i) {
    //   this.nowselecttype = i;
    //   if (i === 0) {
    //     this.search();
    //   } else if (i === 1) {
    //     this.getTableDataListtwo();
    //   } else if (i === 2) {
    //     // this.getTableDataListtwo()
    //   }
    // },
    // 查看详细数据
    getalrmmessage(e) {
      this.$alert(e.message, "详细数据", {
        showConfirmButton: false,
        closeOnClickModal: true,
        // confirmButtonText: '确定',
        confirmButtonClass: "zh-button-two"
        // callback: action => {
        //   this.$message({
        //     type: 'info',
        //     message: `action: ${action}`
        //   });
        // }
      });
      // this.$confirm(e.message)
      //   .then(_ => {
      //     // done();
      //   })
      //   .catch(_ => {});
      // console.log(e)
    },
    // 布尔值格式化
    formatBoolean(row, column, cellValue) {
      var ret = ""; // 你想在页面展示的值
      if (cellValue) {
        ret = "已完成"; // 根据自己的需求设定
      } else if (cellValue === false) {
        ret = "未完成";
      }
      return ret;
    },
    // 获取下拉选择数据
    getselecltlist() {
      // 获取所有监测数据项
      nowpagereq.getdataitemlist().then(res => {
        this.dataitemlist = res.data;
        this.formConfig[4].options = res.data;
      });
      let params = {
        organizationList: sessionStorage
          .getItem("organizationList")
          .split(",")
          .concat([sessionStorage.parentId || ""]),
        farmingTypeNo: sessionStorage.getItem("farmingTypeNo")
      };
      // 获取设施
      userreq.getfacilityAll(params).then(res => {
        this.facilityList = res.data;
        this.formConfig[1].options = res.data;
      });
      nowpagereq.getbatchlist().then(res => {
        this.batchList = res.data;
        this.formConfig[0].options = res.data;
      });
      alarmNowpagereq.getalldatalist().then(res => {
        this.dataTypeList = res.data;
        this.formConfig[3].options = res.data;
      });
      let faciparams = {
        organizationList: sessionStorage
          .getItem("organizationList")
          .split(",")
          .concat([sessionStorage.parentId || ""]),
        farmingTypeNo: sessionStorage.getItem("farmingTypeNo")
      };
      nowpagereqs.querybycondition(faciparams).then(res => {
        this.alarmTypeList = res.data;
        this.formConfig[2].options = res.data;
      });
      // this.dataTypeList = [
      //   {
      //     name: "空气数据",
      //     type: "AIR"
      //   },
      //   {
      //     name: "告警指示灯数据",
      //     type: "ALARM"
      //   },
      //   {
      //     name: "气象站API",
      //     type: "ATMOSPHERE"
      //   },
      //   {
      //     name: "土壤数据",
      //     type: "SOIL"
      //   },
      //   {
      //     name: "设备状态",
      //     type: "STATE"
      //   },
      //   {
      //     name: "大田气象站数据",
      //     type: "WEATHER"
      //   }
      // ]
    },
    // 获取当前数值告警列表
    getTableDataListtwo() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize
      };
      if (
        this.searchlist.dataItemId !== "" ||
        this.searchlist.date.length > 0
      ) {
        let searchparam = this.searchlist;
        if (searchparam.date[0]) {
          searchparam.startTime = this.dateToFormat(searchparam.date[0]);
        }
        if (searchparam.date[1]) {
          searchparam.endTime = this.dateToFormat(searchparam.date[1]);
        }
        nowpagereq.getDataItemquery(this.searchlist).then(res => {
          let data = res.data;
          // 赋值总条数
          this.totalSize = data.length;
          // 赋值数组
          this.tableData = data;
          this.addblankmet();
          this.resetActionBar();
        });
        return;
      }
      nowpagereq.getDataItemtable(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("数值告警表格获取失败");
        }
        let data = res.data.content;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    // 导出
    async download() {
      let params = {
        entity: {
          ...this.entity,
          organizationNo: sessionStorage.getItem("organizationNo"),
          farmingTypeNo: sessionStorage.getItem("farmingTypeNo"),
          system: "SC",
          disposeStatus: true
        }
      }
      let url = 'v3/alarm/alarmMessage/download'
      let excel = '历史告警记录表'
      this.downloadExcel(url, params, excel)
    },
    // 获取当前拥有类型表格
    getTableDataList() {
      // 拦截表格数据请求
      if (this.nowselecttype === 1) {
        this.getTableDataListtwo();
        return;
      }
      this.loading = true;
      let searchparam = {};
      if (this.searchlist.date && this.searchlist.date[0]) {
        searchparam.startTime = this.dateToFormat(this.searchlist.date[0]);
      }
      if (this.searchlist.date && this.searchlist.date[1]) {
        searchparam.endTime = this.dateToFormat(this.searchlist.date[1]);
      }
      searchparam.category = this.searchlist.category || undefined;
      searchparam.batchNo = this.searchlist.batchNo || undefined;
      searchparam.dataTypeId = this.searchlist.dataTypeId || undefined;
      searchparam.dataItemId = this.searchlist.dataItemId || undefined;
      searchparam.alarmTypeId = this.searchlist.alarmTypeId || undefined;
      searchparam.facilityNo = this.searchlist.facilityNo || undefined;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: searchparam,
        sortList: this.sortList || [],
      };
      this.entity = params.entity
      // if (this.searchlist.dataItemId !== '' || this.searchlist.date.length > 0) {
      //   let searchparam = this.searchlist
      //   if (searchparam.date[0]) {
      //     searchparam.startTime = this.dateToFormat(searchparam.date[0])
      //   }
      //   if (searchparam.date[1]) {
      //     searchparam.endTime = this.dateToFormat(searchparam.date[0])
      //   }
      //   nowpagereq.quertbycond(this.searchlist).then(
      //     res => {
      //       let data = res.data
      //       // 赋值总条数
      //       this.totalSize = data.length;
      //       // 赋值数组
      //       this.tableData = data;
      //       this.addblankmet();
      //       this.resetActionBar();
      //     }
      //   )
      //   return
      // }
      nowpagereq.getlist(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("当前告警历史表格获取失败");
        }
        let data = res.data.content;
        // 赋值总条数
        this.totalSize = res.data.totalElements;
        // 赋值数组
        this.tableData = data;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleSelectionChange(list) {
      this.nowselectedList = [];
      this.nowselectedList = list.map(item => {
        return item.ownUnitId;
      });
      if (this.nowselectedList && this.nowselectedList.length > 0) {
        this.delActive = true;
      } else this.delActive = false;
    },
    delmessage() {
      if (this.nowChanceStau === "del") {
        nowpagereq.dellist(this.nowselectedList).then(res => {
          if (res.code === 1) {
            this.search();
            this.$message.success("删除成功");
          } else {
            this.$message.error("删除失败");
          }
        });
      }
    },
    save() {
      // if (this.nowChanceStau === 'add') {
      //   nowpagereq.addUnitlist(this.nowselectedList).then(
      //     res => {
      //       if (res.code === 1) {
      //         this.getTableDataListone()
      //         this.$message.success('添加成功')
      //       } else {
      //         this.$message.error('添加失败')
      //       }
      //     }
      //   )
      // }
    },
    // 查看设施列表
    lookFacilityList(row) {
      this.facilityBOList = row.facilityList
      this.isShowEquip = true
    }
  }
};
</script>
<style lang="scss" scoped>
.zhtabletitle {
  color: #31f7dd;
  margin-bottom: 10px;
}
::v-deep .tab-select {
  width: 100%;
  .butt-list-sty {
    position: absolute;
    left: 5px;
    top: 7px;
  }
}
</style>
